from datetime import timedelta

from django.db.models import Q
from django.utils import timezone

from polls.models import User
from vote import app


@app.task
def display_info(message, count):
    for _ in range(count):
        print(message)
    return True


@app.task
def reset_users_counter():
    User.objects.filter(is_locked=False).update(counter=5)


@app.task
def check_inactive_users():
    thirty_days_ago = timezone.now() - timedelta(days=30)
    User.objects.filter(
        Q(last_visit__isnull=True) | Q(last_visit__lt=thirty_days_ago)
    ).filter(is_locked=False).update(is_locked=True)
